package com.zhuozhou.jdyc.main.main.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;

import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.api.UserAccountHelper;
import com.zhuozhou.jdyc.base.BaseActivity;
import com.zhuozhou.jdyc.databinding.ActivitySearchBinding;
import com.zhuozhou.jdyc.main.main.bean.BasicsCitySearchBean;
import com.zhuozhou.jdyc.main.main.viewmodel.MainViewModel;
import com.zhuozhou.jdyc.util.apiUtil.StringUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Create by Once_ChuXy on 2021/8/17
 * Describe:搜索
 */
public class SearchActivity extends BaseActivity<MainViewModel, ActivitySearchBinding> {

    private String searchRecords;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search;
    }

    @Override
    public String setTitleBar() {
        return "搜索";
    }

    @Override
    public void initView() {
        setSearchRecords();
        binding.searchEdit.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        binding.searchEdit.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                try {
                    if (StringUtil.isNotEmpty(searchRecords))
                        searchRecords = v.getText().toString() + "," + searchRecords;
                    else searchRecords = v.getText().toString();
                    UserAccountHelper.saveSearchRecords(searchRecords);
                    Intent in = new Intent(this, GoodsClassifyActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("searchEdit", v.getText().toString());
                    bundle.putInt("type", 1);
                    in.putExtras(bundle);
                    startActivity(in);
                    // 当按了搜索之后关闭软键盘
                    ((InputMethodManager) binding.searchEdit.getContext().getSystemService(
                            Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
                            this.getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
                    setSearchRecords();
                    return true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return false;
        });
        binding.labels.setOnLabelClickListener((label, data, position) -> {
            binding.searchEdit.setText(data.toString());
        });
        binding.labels0.setOnLabelClickListener((label, data, position) -> {
            binding.searchEdit.setText(data.toString());
        });
        binding.imageDel.setOnClickListener(v -> {
            UserAccountHelper.saveSearchRecords("");
            searchRecords = UserAccountHelper.getSearchRecords();
            setSearchRecords();
        });
        mViewModel.shopBasicsCitySearchMutableLiveData.observe(this, v -> {
            List<String> labelList = new ArrayList<>();
            for (BasicsCitySearchBean item : v) {
                labelList.add(item.getName());
            }
            binding.labels0.setLabels(labelList);
        });
    }

    public void setSearchRecords() {
        searchRecords = UserAccountHelper.getSearchRecords();
        if (StringUtil.isNotEmpty(searchRecords)) {
            List<String> list = new ArrayList<>(Arrays.asList(searchRecords.split(",")));
            binding.labels.setLabels(list);
            binding.labels.setVisibility(View.VISIBLE);
        } else {
            binding.labels.setVisibility(View.GONE);
        }
    }

    @Override
    public void initData(Bundle bundle) {
        mViewModel.getBasicsCitySearch(UserAccountHelper.getNewCity());
    }
}
